//=============================================================================

#include "../../../../inc/aPacketIni.h"

//=============================================================================

aPacketIni::aPacketIni() : aPacket()
{
  version = 0.0f;
  serverToClient = 0;
  clientToServer = 0;
  type = PAK_INI;
}

//=============================================================================

aPacketIni::aPacketIni(char* in = 0) : aPacket(in)
{
  version = 0.0f;
  serverToClient = 0;
  clientToServer = 0;
  apApartPacket();
}

//=============================================================================

aPacketIni::aPacketIni(const aPacketIni& p) : aPacket(p)
{
  apApartPacket();
}

//=============================================================================

aPacketIni::aPacketIni(float ver, bool stc, bool cts) : aPacket()
{
  version = ver;
  serverToClient = stc;
  clientToServer = cts;
  apPreparePacket();
}

//=============================================================================

void aPacketIni::apApartPacket()
{
  if (!data)
    return;

  size_t step = 1 + longSize;
  //Packet data
  float ver;

  //Look version
  memcpy(&ver, &data[step], floatSize);
  step += floatSize;

  memcpy(&serverToClient, &data[step], boolSize);
  step += boolSize;

  memcpy(&clientToServer, &data[step], boolSize);
  step += boolSize;

  //apply
  version = ver;
}

//=============================================================================

void aPacketIni::apPreparePacket()
{
  apClearPacket();

  size_t step = 0;

  length = 1 + longSize + floatSize + boolSize*2;
  type = PAK_INI;
  data = new char[length + 1];
  data[step] = type;
  step++;

  memcpy(&data[step], &length, longSize);
  step += longSize;

  memcpy(&data[step], &version, floatSize);
  step += floatSize;

  memcpy(&data[step], &serverToClient, boolSize);
  step += boolSize;

  memcpy(&data[step], &clientToServer, boolSize);
  step += boolSize;

  data[length] = 0;
}

//=============================================================================

const aPacketIni& aPacketIni::operator=(const aPacketIni& p)
{
  apClearPacket();

  this->version = p.version;
  this->serverToClient = p.serverToClient;
  this->clientToServer = p.clientToServer;

  if (p.apGetLength() <= 0)
    return (*this);

  length = p.apGetLength();
  data = new char[length];
  memcpy(data, p.apGetData(), length);
  type = p.apGetType();

  return (*this);
}

//=============================================================================

aPacketIni::~aPacketIni()
{
  apClearPacket();
}

//=============================================================================
